#include "ClientDialog.h"
#include "ui_ClientDialog.h"

ClientDialog::ClientDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ClientDialog)
{
    ui->setupUi(this);
    //初始化标记 默认是离线状态
    status = false;
    //连接服务器时发送信号 connected
    connect (&tcpsocket, SIGNAL(connected()),
             this, SLOT(on_Connected()));
    //断开服务器连接时发送的信号
    connect (&tcpsocket, SIGNAL(disconnected()),
            this, SLOT(on_Disconnected()));
    //网络异常时发送的信号 error
    connect (&tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)),
             this, SLOT(on_Error(QAbstractSocket::SocketError)));
    //接受到socket的消息时,发送信号 readyRead
    connect (&tcpsocket, SIGNAL(readyRead()),
             this, SLOT(on_ReadyRead()));
}

ClientDialog::~ClientDialog()
{
    delete ui;
}

//点击发送按钮执行的槽函数
void ClientDialog::on_SendButton_clicked()
{
    //获取输入的聊天消息
    QString msg = ui->MessageEdit->text();
    if (msg == "") {
        return ;
    }
    msg = username + ":" + msg;
    tcpsocket.write(msg.toUtf8());
    //清空输入
    ui->MessageEdit->clear();
}

//点击连接按钮执行的槽函数
void ClientDialog::on_ConnectButton_clicked()
{
    if (status == false) {
        //当前如果是离线状态则连接服务器
        //服务器的ip
        QString ip = ui->ServeripEdit->text();
        if (serverIP.setAddress(ip) == false) {
            QMessageBox::critical (this, "Error", "Server IP Error!");
            return ;
        }
        //获取服务器的端口
        port = ui->ServerportEdit->text().toShort();
        if (port < 1024 || port > 60000) {
            QMessageBox::critical (this, "Error", "Server port Error!(1024~60000)");
            return ;
        }
        //获取聊天室的昵称
        username = ui->NameEdit->text();
        if (username == "") {
            QMessageBox::critical (this, "Error", "聊天室昵称不能为空!");
            return ;
        }
        //连接服务器
        tcpsocket.connectToHost(serverIP, port);
    } else {
       //当前在线则断开
       QString msg = username + ":" + "离开聊天室";
       tcpsocket.write(msg.toUtf8());
       tcpsocket.disconnectFromHost();
    }
}

//连接服务器成功执行的槽函数
void ClientDialog::on_Connected(void)
{
    status = true; //标记：在线
    //使能发送消息的按钮
    ui->SendButton->setEnabled(true);
    ui->ServeripEdit->setEnabled(false);
    ui->ServerportEdit->setEnabled(false);
    ui->NameEdit->setEnabled(false);

    ui->ConnectButton->setText("离开聊天室");
    //向服务器发送进入聊天室的提示信息
    QString str = username + ":" + "进入聊天室";
    tcpsocket.write(str.toUtf8());
}

//断开服务器连接执行的槽函数
void ClientDialog::on_Disconnected(void)
{
    //标记信息
    status = false;
    ui->SendButton->setEnabled(false);
    ui->ServeripEdit->setEnabled(true);
    ui->ServerportEdit->setEnabled(true);
    ui->NameEdit->setEnabled(true);

    ui->ConnectButton->setText("连接服务器");
}

//网络异常时执行的槽函数
void ClientDialog::on_Error (QAbstractSocket::SocketError)
{
    qDebug () << tcpsocket.errorString();
    QMessageBox::critical(
                this, "ERROR", tcpsocket.errorString());
}

//接受消息的槽函数
void ClientDialog::on_ReadyRead(void)
{
    //获取等待读取消息的字节数
    if(tcpsocket.bytesAvailable()) {
        QByteArray readBuf;
        readBuf.resize (tcpsocket.bytesAvailable());
        tcpsocket.read(readBuf.data(), readBuf.size());

        //显示
        ui->listWidget->addItem(readBuf);
        //回滚到底部
        ui->listWidget->scrollToBottom();
    }
}

